package com.xwaf.platform.web.controller.pay;

import java.io.BufferedOutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.request.three.AliProductRequest;
import com.xwaf.platform.system.api.service.pay.AliService;
import com.xwaf.platform.system.api.service.pay.OrderService;
import com.xwaf.platform.system.api.util.AliPayConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@Api(tags = "阿里")
@RequestMapping(value = "/x/ali/")
public class AliController extends BaseController {

	@Autowired
	private AliService aliService;
	@Autowired
	private OrderService orderService;

	@ApiOperation(value = "电脑支付")
	@RequestMapping(value = "pcPay", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public CommonResponse pcPay(@RequestBody AliProductRequest product) {
		log.info("电脑支付");
		String form = aliService.aliPayPc(product);
		return CommonResponse.createCommonResponse(form);
	}

	@ApiOperation(value = "手机H5支付")
	@RequestMapping(value = "mobilePay", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public CommonResponse mobilePay(@RequestBody AliProductRequest product) {
		log.info("手机H5支付");
		String form = aliService.aliPayMobile(product);
		return CommonResponse.createCommonResponse(form);
	}

	/**
	 * 支付宝支付后台回调(二维码、H5、网站)
	 */
	@ApiOperation(value = "支付宝支付回调(二维码、H5、网站)")
	@RequestMapping(value = "pay", method = RequestMethod.POST)
	public void alipayNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String message = "success";
		Map<String, String> params = new HashMap<String, String>();
		// 取出所有参数是为了验证签名
		Enumeration<String> parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String parameterName = parameterNames.nextElement();
			params.put(parameterName, request.getParameter(parameterName));
		}
		String meid = orderService.checkOrder(params);
		// 验证签名 校验签名
		boolean signVerified = false;
		try {
			signVerified = AlipaySignature.rsaCheckV1(params, meid, AliPayConfig.CHARSET, AliPayConfig.SIGN_TYPE);
		} catch (AlipayApiException e) {
			e.printStackTrace();
			message = "failed";
		}
		if (signVerified) {
			log.info("支付宝验证签名成功！");
			// 若参数中的appid和填入的appid不相同，则为异常通知
			// if (!Configs.getAppid().equals(params.get("app_id"))) {
			// log.info("与付款时的appid不同，此为异常通知，应忽略！");
			// message = "failed";
			// }else{
			String outtradeno = params.get("out_trade_no");
			// 在数据库中查找订单号对应的订单，并将其金额与数据库中的金额对比，若对不上，也为异常通知
			String status = params.get("trade_status");
			if (status.equals("WAIT_BUYER_PAY")) { // 如果状态是正在等待用户付款
				log.info(outtradeno + "订单的状态正在等待用户付款");
			} else if (status.equals("TRADE_CLOSED")) { // 如果状态是未付款交易超时关闭，或支付完成后全额退款
				log.info(outtradeno + "订单的状态已经关闭");
			} else if (status.equals("TRADE_SUCCESS") || status.equals("TRADE_FINISHED")) { // 如果状态是已经支付成功
				log.info("(支付宝订单号:" + outtradeno + "付款成功)");
				// 这里 根据实际业务场景 做相应的操作
				orderService.notifyOrder(params);
			} else {

			}
			// }
		} else { // 如果验证签名没有通过
			message = "failed";
			log.info("验证签名失败！");
		}
		BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
		out.write(message.getBytes());
		out.flush();
		out.close();
	}
}
